﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Models;
using BLL;
using System.Net;

namespace Dryricenet.houTai
{
    public partial class user_data : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string name = Session["UserName"].ToString();
                //根据id查询数据，并展示在输入框中
                DataTable dt = userBLL.selectNameBll(name);
                //用DataRow来存一行数据
                DataRow dr = dt.Rows[0];//第一条数据
                                        //把数据一个一个的放进输入框里
                txtName.Text = dr["UserName"].ToString();
                txtAge.Text = dr["Age"].ToString();
                txtPhone.Text = dr["Phone"].ToString();
                string num = dr["UserTypeID"].ToString();
                if (num == "1")
                {
                    txtType.Text = "用户";
                }
                else {
                    txtType.Text = "管理员";
                }
                txtZT.Text= dr["Titles"].ToString();
                //性别处理
                if (dr["Sex"].ToString() == "男")
                {
                    rdbNan.Checked = true;
                }
                else
                {
                    rdbNv.Checked = true;
                }
            }
        }
        //提交
        protected void Update_Button_Click(object sender, EventArgs e)
        {
            string name = Session["UserName"].ToString();
            string sex = "";
            int age = 0;
            if (rdbNan.Checked)
            {
                sex = "男";
            }
            else {
                sex = "女";
            }
            if (txtName.Text.Trim() != "" && txtAge.Text.Trim() != "" && txtPhone.Text.Trim() != "" && txtType.Text.Trim() != "" && txtZT.Text.Trim() != "")
            {
                age = Convert.ToInt32(txtAge.Text.Trim());
                if (age > 10 && age < 108)
                {
                    User us = new User();
                    us.UserName = txtName.Text.Trim();
                    us.Sex = sex;
                    us.Age = age;
                    us.Phone = txtPhone.Text.Trim();
                    if (userBLL.updateUserBLL(us, name))
                    {
                        Session["UserName"] = txtName.Text.Trim();

                        string names = Session["UserName"].ToString();
                        System.Data.DataTable dt = userBLL.selectNameBll(names);
                        DataRow dr = dt.Rows[0];//第一条数据
                        int userID = Convert.ToInt32(dr["UserID"].ToString());
                        Log l = new Log();
                        l.OperTime = DateTime.Now;
                        l.OperPerson = name;
                        l.Type = "添加用户";
                        l.State = "添加成功";
                        l.ComputerName = Dns.GetHostName().ToUpperInvariant();
                        l.System = Systems.GetOSNameByUserAgent(Environment.OSVersion.ToString());
                        l.UserID = userID;
                        LogBLL.LogAddBLL(l);
                        Response.Write("<script>javascript:alert('修改成功!');history.back();</script>");
                        Response.End();
                    }
                    else {
                        Response.Write("<script>javascript:alert('修改失败!');history.back();</script>");
                        Response.End();
                    }
                }
                else {
                    Response.Write("<script>javascript:alert('年龄输入不合法!');history.back();</script>");
                    Response.End();
                }
            }
            else {
                Response.Write("<script>javascript:alert('输入不能为空,请重新输入!');history.back();</script>");
                Response.End();
            }
        }
    }
}